package com.shinemo.mpush.alloc.service;

import java.util.Date;
import java.util.Map;

import com.mpush.api.chat.ChatMessage;
import com.mpush.api.chat.ChatMessageSendResult;
import com.mpush.netty.http.HttpClient;
import com.mpush.netty.http.NettyHttpClientUtils;
import com.mpush.tools.common.DateUtils;
import com.mpush.tools.config.CC;
import com.shinemo.mpush.alloc.http.MessageHttpCallback;

public class MessageService {
	
	/**
	 * 保存消息
	 * @param httpClient
	 * @param message
	 * @param result
	 * @throws Exception
	 */
	public void saveMsg(HttpClient httpClient, ChatMessage message, ChatMessageSendResult result) throws Exception {

		String uri = "http://" + CC.mp.api.server_address + CC.mp.api.saveMsg;

		Map<String, String> param = message.toParam();

		param.put("sendStatue", "" + result.value);
		param.put("receiveStatus", "" + result.value);

		String sendTime = DateUtils.format(new Date(message.getSendTime()),DateUtils.FULL_TIME_FORMAT);
		param.put("sendTime", sendTime);
		if(result==ChatMessageSendResult.SUCCESS) {
			param.put("receiveTime", sendTime);
		}

		NettyHttpClientUtils.post(httpClient, uri, param, new MessageHttpCallback(message));
	}
}
